<?php

defined('IN_SCHOOL') or die();

/*{TranslateTagBegin}*/
class control_vcode extends base_control{

	public function __construct() {
	
		parent::__construct();
		
	}
	

	public function do_index() {
		session_start();
		$im = imagecreate(100, 25);
	
		//imagecolorallocate($im, 14, 114, 180); // background color
		$red = imagecolorallocate($im, 255, 0, 0);
		$white = imagecolorallocate($im, 255, 255, 255);
	
		$num1 = rand(1, 20);
		$num2 = rand(1, 20);
	
		$_SESSION['JY_VCODE'] = $num1 + $num2;
	
		$gray = imagecolorallocate($im, 118, 151, 199);
		$black = imagecolorallocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
	
		imagefilledrectangle($im, 0, 0, 100, 24, $black);
		for ($i = 0; $i < 80; $i++) {
			imagesetpixel($im, rand(0, 100), rand(0, 25), $gray);
		}
	
		imagestring($im, 5, 5, 4, $num1, $red);
		imagestring($im, 5, 30, 3, "+", $red);
		imagestring($im, 5, 45, 4, $num2, $red);
		imagestring($im, 5, 70, 3, "=", $red);
		imagestring($im, 5, 80, 2, "?", $white);
	
		header("Content-type: image/png");
		imagepng($im);
		imagedestroy($im);
	}
	
	public function do_set() {
		$this->check_gid100();
		
		$edit_msg = '';
		
		$content = core::gpc('content', 'P');
		if(!empty($content)) {
			$content = max(1,intval($content));
			$edit_msg = '设置失败';
			if($this->kv->set('jy_vcodelimit', $content)) {
				$edit_msg = '设置成功';
			}
		}
		
		$content = $this->kv->get('jy_vcodelimit');
		$content = $content == false ? 1 : $content;
		
		$this->tpl->assign('edit_msg',$edit_msg);
		$this->tpl->assign('content',$content);
		$this->tpl->display('vcode_set.tpl');
	}
	
	private function check_gid100() {
		$gid = $this->user->user_groupid();
		if($gid != 100)
			$this->message('权限不足','index.php');
	}

}
/*{TranslateTagEnd}*/

?>